package com.zgm.utils.ui;

/**
 * Created by 张公铭 on 2017/6/23 0023.
 */

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.zgm.utils.R;


/**
 * Created by 张公铭 on 2017/5/19 0019.
 */

public class LoadingDialog extends Dialog
{
    private Context mContext;
    private View mView;
    private ImageView iv_loading_icon;
    private TextView tv_text;
    private AnimationDrawable loadingAnima;

    public LoadingDialog(@NonNull Context context, int backgourdResId)
    {
        super(context, R.style.LoadingDialog);
        super.setCancelable(false);
        mContext = context;
        init(backgourdResId);
    }


    private void init(int resId)
    {
        mView = LayoutInflater.from(mContext).inflate(R.layout.loading, null);
        iv_loading_icon = (ImageView) mView.findViewById(R.id.iv_loading_icon);
        iv_loading_icon.setBackgroundResource(resId);
        loadingAnima = (AnimationDrawable) iv_loading_icon.getBackground();
        super.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.setContentView(mView);
        super.getWindow().setBackgroundDrawable(new BitmapDrawable(mContext.getResources()));

        tv_text = (TextView) mView.findViewById(R.id.tv_loading_text);
        tv_text.setVisibility(View.GONE);
    }

    public void setText(String text)
    {
        tv_text.setVisibility(View.VISIBLE);
        tv_text.setText(text);
    }

    @Override
    public void show()
    {
        super.show();
        loadingAnima.setOneShot(false);
        loadingAnima.start();
    }

    @Override
    public void dismiss()
    {
        super.dismiss();
        loadingAnima.stop();
    }
}
